#! /usr/bin/env python

# -*- coding: UTF-8 -*-
# ==============================================================================
# Filename : app.py
# Purpose  : 
# Author   : Yohann Li (lyh001686@sina.com)
# Date     : 2020-07-01 17:26
# ==============================================================================
import sys, os

from PyQt5.QtCore import Qt
from PyQt5.QtGui import QFont, QFontDatabase, QIcon
from PyQt5.QtWidgets import QApplication

from Widgets.MainUI import UI
from conf.configuration import style, logger, VERSION, APPNAME, img_path, APPPIC


def get_geometry(app: QApplication):
    win_w = app.desktop().screen().width()
    win_h = app.desktop().screen().height()
    w = 1000
    h = 750
    return (win_w - w) / 2, (win_h - h) / 2, w, h


class App(QApplication):
    def __init__(self):
        super(App, self).__init__(sys.argv)
        self.fontChanged.connect(self.font_changed)

    @staticmethod
    def font_changed(font: QFont):
        logger.info(f"font changed: {font.toString()}")


if __name__ == '__main__':
    try:
        app = App()
        app.setAttribute(Qt.AA_UseHighDpiPixmaps, True)
        app.setWindowIcon(QIcon(APPPIC))
        app.setStyleSheet(style)
        app.setApplicationVersion(VERSION)
        app.setApplicationName(APPNAME)
        font_ = QFont("Menlo")
        app.setFont(font_)
        # fdb = QFontDatabase()
        # print(fdb.families(QFontDatabase.Any))
        # print(app.font().rawName())
        # print(app.font().family())
        # print(app.font().families())
        # print(app.font().defaultFamily())
        # print(app.font().style())
        # print(app.font().styleName())
        # print(app.font().toString())
        # print(app.font().key())
        ui = UI()
        ui.show()
        ui.check_updates(silent=True)
        ui.show_refer_notes()
        x, y, w, h = get_geometry(app)
        ui.setGeometry(int(x), int(y), w, h)
        sys.exit(app.exec_())
    except Exception as e:
        logger.error(e, exc_info=1)
